﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nuctech.ACCT.OIS.SqlSugar.AdaptAbpRepository;
using Nuctech.ACCT.OIS.SqlSugar.Client;
using Nuctech.ACCT.OIS.SqlSugar.Repository;
using System;
using System.Linq;
using Volo.Abp.Modularity;

namespace Nuctech.ACCT.OIS.SqlSugar
{
    public class NuctechSqlSugarModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            var section = configuration.GetSection(DbSettingOption.DbSettingPath);
            if (section == null)
            {
                throw new ArgumentException("数据库配置为空");
            }
            Configure<DbSettingOption[]>(section);
            var dbConfigs = section.Get<DbSettingOption[]>();

            if (dbConfigs?.Count() <= 0)
            {
                throw new ArgumentException("数据库配置为空");
            }

            // 创建客户端
            context.Services.AddSqlSugarClient(dbConfigs);

            // 原生仓储
            context.Services.AddTransient(typeof(SqlSugarRepository<>));

            // 兼容IRepository仓储
            //context.Services.AddTransient(typeof(IRepository<>), typeof(SqlSugarAdaptationAbpRepository<>));
            //context.Services.AddTransient(typeof(IRepository<,>), typeof(SqlSugarAdaptationAbpRepository<,>));

            // 目标：集大成仓储
            context.Services.AddTransient(typeof(ISqlSugarRepository<>), typeof(SqlSugarAdaptationAbpRepository<>));
            context.Services.AddTransient(typeof(ISqlSugarRepository<,>), typeof(SqlSugarAdaptationAbpRepository<,>));
        }
    }

}
